namespace DotNetNuke.Security.Roles
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Data;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Entities.Users;
    using DotNetNuke.Services.Exceptions;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Data;
    using System.Runtime.CompilerServices;
    using System.Web.Security;

    /// -----------------------------------------------------------------------------
    /// Project:    DotNetNuke
    /// Namespace:  DotNetNuke.Security.Roles
    /// Class:      RoleController
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The RoleController class provides Business Layer methods for Roles
    /// </summary>
    /// <history>
    /// [cnurse]    05/23/2005  made compatible with .NET 2.0
    /// </history>
    /// -----------------------------------------------------------------------------
    public class RoleController
    {
        /// <summary>
        /// Add a new role
        /// </summary>
        /// <param name="objRoleInfo">The Role to Add</param>
        /// <returns>The Id of the new role</returns>
        /// <history>
        /// [cnurse]	05/23/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public int AddRole(RoleInfo objRoleInfo)
        {
            return this.AddRole(objRoleInfo, false);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// This overload adds a role and optionally adds the info to the AspNet Roles
        /// </summary>
        /// <param name="objRoleInfo">The Role to Add</param>
        /// <param name="SynchronizationMode">Flag indicating whether the role should be
        /// added to the AspNet Roles</param>
        /// <returns>The Id of the new role</returns>
        /// <history>
        /// [cnurse]	05/23/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public int AddRole(RoleInfo objRoleInfo, bool SynchronizationMode)
        {
            int AddRole;
            string originalAppName = DotNetNuke.Common.Globals.GetApplicationName();
            try
            {
                if (!SynchronizationMode)
                {
                    DotNetNuke.Common.Globals.SetApplicationName(objRoleInfo.PortalID);
                    Roles.CreateRole(objRoleInfo.RoleName);
                }
                int RoleId = -1;
                RoleId = DataProvider.Instance().AddRole(objRoleInfo.PortalID, objRoleInfo.RoleName, objRoleInfo.Description, objRoleInfo.ServiceFee, objRoleInfo.BillingPeriod.ToString(), objRoleInfo.BillingFrequency, objRoleInfo.TrialFee, objRoleInfo.TrialPeriod, objRoleInfo.TrialFrequency, objRoleInfo.IsPublic, objRoleInfo.AutoAssignment);
                this.AutoAssignUsers(objRoleInfo, RoleId, SynchronizationMode);
                AddRole = RoleId;
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(originalAppName);
            }
            return AddRole;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Adds a User to a Role
        /// </summary>
        /// <param name="PortalID">The Id of the Portal</param>
        /// <param name="UserId">The Id of the User</param>
        /// <param name="RoleId">The Id of the Role</param>
        /// <param name="ExpiryDate">The expiry Date of the Role membership</param>
        /// <history>
        /// [cnurse]	05/24/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public void AddUserRole(int PortalID, int UserId, int RoleId, DateTime ExpiryDate)
        {
            string originalAppName = "";
            UserInfo objUser = new UserController().GetUser(PortalID, UserId);
            UserRoleInfo objUserRole = this.GetUserRole(PortalID, UserId, RoleId);
            if (objUserRole == null)
            {
                originalAppName = DotNetNuke.Common.Globals.GetApplicationName();
                DotNetNuke.Common.Globals.SetApplicationName(PortalID);
                DataProvider.Instance().AddUserRole(PortalID, UserId, RoleId, ExpiryDate);
                objUserRole = this.GetUserRole(PortalID, UserId, RoleId);
                try
                {
                    Roles.AddUserToRole(objUser.Membership.Username, objUserRole.RoleName);
                }
                catch (Exception exception1)
                {
                    
                    Exception ex = exception1;
                    DotNetNuke.Common.Globals.SetApplicationName(originalAppName);
                    
                }
            }
            else
            {
                DataProvider.Instance().UpdateUserRole(objUserRole.UserRoleID, ExpiryDate);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Auto Assign existing users to a role
        /// </summary>
        /// <param name="objRoleInfo">The Role to Auto assign</param>
        /// <param name="RoleId">The Id of the Role</param>
        /// <param name="SynchronizationMode">not used</param>
        /// <history>
        /// [cnurse]	05/23/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        private void AutoAssignUsers(RoleInfo objRoleInfo, int RoleId, bool SynchronizationMode)
        {
            if (objRoleInfo.AutoAssignment)
            {
                IDataReader dr = DataProvider.Instance().GetUsers(objRoleInfo.PortalID);
                while (dr.Read())
                {
                    try
                    {
                        this.AddUserRole(objRoleInfo.PortalID, Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["UserId"])), RoleId, Null.NullDate);
                    }
                    catch (Exception exception1)
                    {
                        
                        Exception ex = exception1;
                        
                    }
                }
                dr.Close();
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Delete a Role
        /// </summary>
        /// <param name="RoleId">The Id of the Role to delete</param>
        /// <param name="PortalId">The Id of the Portal</param>
        /// <history>
        /// [cnurse]	05/24/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public void DeleteRole(int RoleId, int PortalId)
        {
            string originalAppName = DotNetNuke.Common.Globals.GetApplicationName();
            try
            {
                RoleInfo objRole = this.GetRole(RoleId, PortalId);
                DotNetNuke.Common.Globals.SetApplicationName(PortalId);
                string[] users = Roles.GetUsersInRole(objRole.RoleName);
                if (users.Length > 0)
                {
                    Roles.RemoveUsersFromRole(users, objRole.RoleName);
                }
                Roles.DeleteRole(objRole.RoleName);
                DataProvider.Instance().DeleteRole(RoleId);
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(originalAppName);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Delete/Remove a User from a Role
        /// </summary>
        /// <param name="PortalID">The Id of the Portal</param>
        /// <param name="UserId">The Id of the User</param>
        /// <param name="RoleId">The Id of the Role</param>
        /// <returns></returns>
        /// <history>
        /// [cnurse]	05/24/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public bool DeleteUserRole(int PortalId, int UserId, int RoleId)
        {
            bool DeleteUserRole;
            string originalAppName = DotNetNuke.Common.Globals.GetApplicationName();
            try
            {
                UserInfo objUser = new UserController().GetUser(PortalId, UserId);
                UserRoleInfo objUserRole = this.GetUserRole(PortalId, UserId, RoleId);
                PortalController objPortals = new PortalController();
                bool blnDelete = true;
                PortalInfo objPortal = objPortals.GetPortal(PortalId);
                if (objPortal != null)
                {
                    if (((objPortal.AdministratorId != UserId) | (objPortal.AdministratorRoleId != RoleId)) & (objPortal.RegisteredRoleId != RoleId))
                    {
                        DotNetNuke.Common.Globals.SetApplicationName(PortalId);
                        Roles.RemoveUserFromRole(objUser.Membership.Username, objUserRole.RoleName);
                        DataProvider.Instance().DeleteUserRole(UserId, RoleId);
                    }
                    else
                    {
                        blnDelete = false;
                    }
                }
                DeleteUserRole = blnDelete;
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(originalAppName);
            }
            return DeleteUserRole;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Get a list of roles for the Portal
        /// </summary>
        /// <param name="PortalId">The Id of the Portal</param>
        /// <returns>An ArrayList of RoleInfo objects</returns>
        /// <history>
        /// [cnurse]	05/24/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public ArrayList GetPortalRoles(int PortalId)
        {
            return this.GetPortalRoles(PortalId, false);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// This overloads get a list of roles and optionally synchronizes the DNN and
        /// AspNet Roles
        /// </summary>
        /// <param name="PortalId">The Id of the Portal</param>
        /// <param name="SynchronizeRoles">Flagg that indicates whether the DNN and
        /// AspNet roles should be Synchronized</param>
        /// <returns>An ArrayList of RoleInfo objects</returns>
        /// <history>
        /// [cnurse]	05/24/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public ArrayList GetPortalRoles(int PortalId, bool SynchronizeRoles)
        {
            if (SynchronizeRoles)
            {
                this.SynchronizeRoles(PortalId);
            }
            return CBO.FillCollection(DataProvider.Instance().GetPortalRoles(PortalId), typeof(RoleInfo));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a list of Roles for a User
        /// </summary>
        /// <param name="UserId">The Id of the User</param>
        /// <param name="PortalID">The Id of the Portal</param>
        /// <returns>A string that contains a list of Roles fro the User delimited by "|"</returns>
        /// <history>
        /// [cnurse]	05/24/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public string[] GetPortalRolesByUser(int UserId, int PortalId)
        {
            string[] GetPortalRolesByUser;
            string strRoles = "";
            IDataReader dr = DataProvider.Instance().GetRolesByUser(UserId, PortalId);
            try
            {
                while (dr.Read())
                {
                    strRoles = strRoles + Conversions.ToString(dr["RoleName"]) + "|";
                }
                if (strRoles.IndexOf("|") > 0)
                {
                    return Strings.Split(Strings.Left(strRoles, strRoles.Length - 1), "|", -1, CompareMethod.Binary);
                }
                GetPortalRolesByUser = null;
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return GetPortalRolesByUser;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Fetch a single Role
        /// </summary>
        /// <param name="RoleID">The Id of the Role</param>
        /// <param name="PortalID">The Id of the Portal</param>
        /// <returns></returns>
        /// <remarks></remarks>
        /// <history>
        /// [cnurse]	05/24/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public RoleInfo GetRole(int RoleID, int PortalID)
        {
            return (RoleInfo) CBO.FillObject(DataProvider.Instance().GetRole(RoleID, PortalID), typeof(RoleInfo));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Obtains a role given the role name
        /// </summary>
        /// <param name="PortalId">Portal indentifier</param>
        /// <param name="RoleName">Name of the role to be found</param>
        /// <returns>A RoleInfo object is the role is found</returns>
        /// <history>
        /// [VMasanas]	27/08/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public RoleInfo GetRoleByName(int PortalId, string RoleName)
        {
            return this.GetRoleByName(PortalId, RoleName, false);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// This overload obtains a role and optionaly synchronizes the DNN and AspNet Roles
        /// </summary>
        /// <param name="PortalId">The Id of the Portal</param>
        /// <param name="RoleName">Name of the role to be found</param>
        /// <param name="SynchronizeRoles"></param>
        /// <returns>A RoleInfo object is the role is found</returns>
        /// <history>
        /// [cnurse]	05/24/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public RoleInfo GetRoleByName(int PortalId, string RoleName, bool SynchronizeRoles)
        {
            if (SynchronizeRoles)
            {
                this.SynchronizeRoles(PortalId);
            }
            return (RoleInfo) CBO.FillObject(DataProvider.Instance().GetRoleByName(PortalId, RoleName), typeof(RoleInfo));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Returns an array of rolenames for a Portal
        /// </summary>
        /// <param name="PortalId">The Id of the Portal</param>
        /// <returns>A String Array of Role Names</returns>
        /// <history>
        /// [cnurse]	05/24/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public string[] GetRoleNames(int PortalID)
        {
            string[] GetRoleNames;
            string originalAppName = DotNetNuke.Common.Globals.GetApplicationName();
            try
            {
                DotNetNuke.Common.Globals.SetApplicationName(PortalID);
                GetRoleNames = Roles.GetAllRoles();
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);
                GetRoleNames = null;
                
                return GetRoleNames;
                
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(originalAppName);
            }
            return GetRoleNames;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets an ArrayList of Roles
        /// </summary>
        /// <returns>An ArrayList of Roles</returns>
        /// <history>
        /// [cnurse]	05/24/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public ArrayList GetRoles()
        {
            return CBO.FillCollection(DataProvider.Instance().GetRoles(), typeof(RoleInfo));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a List of Roles for a given User
        /// </summary>
        /// <param name="UserId">The Id of the User</param>
        /// <param name="PortalId">The Id of the Portal</param>
        /// <returns>A String Array of Role Names</returns>
        /// <history>
        /// [cnurse]	05/24/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public string[] GetRolesByUser(int UserId, int PortalId)
        {
            string[] GetRolesByUser;
            string OriginalApplicationName = DotNetNuke.Common.Globals.GetApplicationName();
            try
            {
                DotNetNuke.Common.Globals.SetApplicationName(PortalId);
                UserController objUserController = new UserController();
                GetRolesByUser = Roles.GetRolesForUser(objUserController.GetUser(PortalId, UserId).Membership.Username);
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(OriginalApplicationName);
            }
            return GetRolesByUser;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a list of Services for a Portal
        /// </summary>
        /// <param name="PortalId">The Id of the Portal</param>
        /// <returns>An ArrayList of UserRoleInfo objects</returns>
        /// <history>
        /// [Nik Kalyani]	10/15/2004	Created multiple signatures to eliminate Optional parameters
        /// </history>
        /// -----------------------------------------------------------------------------
        public ArrayList GetServices(int PortalId)
        {
            return this.GetServices(PortalId, -1);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a list of Services for a User
        /// </summary>
        /// <param name="PortalId">The Id of the Portal</param>
        /// <param name="UserId">The Id of the User</param>
        /// <returns>An ArrayList of UserRoleInfo objects</returns>
        /// <history>
        /// [Nik Kalyani]	10/15/2004	Created multiple signatures to eliminate Optional parameters
        /// </history>
        /// -----------------------------------------------------------------------------
        public ArrayList GetServices(int PortalId, int UserId)
        {
            return CBO.FillCollection(DataProvider.Instance().GetServices(PortalId, UserId), typeof(UserRoleInfo));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a User/Role
        /// </summary>
        /// <param name="PortalID">The Id of the Portal</param>
        /// <param name="UserId">The Id of the user</param>
        /// <param name="RoleId">The Id of the Role</param>
        /// <returns>A UserRoleInfo object</returns>
        /// <history>
        /// [cnurse]	05/24/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public UserRoleInfo GetUserRole(int PortalID, int UserId, int RoleId)
        {
            return (UserRoleInfo) CBO.FillObject(DataProvider.Instance().GetUserRole(PortalID, UserId, RoleId), typeof(UserRoleInfo));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a List of UserRoles by UserName and RoleName
        /// </summary>
        /// <param name="PortalID">The Id of the Portal</param>
        /// <param name="Username">The username of the user</param>
        /// <param name="Rolename">The role name</param>
        /// <returns>An ArrayList of UserRoleInfo objects</returns>
        /// <history>
        /// [cnurse]	05/24/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public ArrayList GetUserRolesByUsername(int PortalID, string Username, string Rolename)
        {
            return CBO.FillCollection(DataProvider.Instance().GetUserRolesByUsername(PortalID, Username, Rolename), typeof(UserRoleInfo));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a List of UserRoles by RoleName
        /// </summary>
        /// <param name="PortalID">The Id of the Portal</param>
        /// <param name="Rolename">The role name</param>
        /// <returns>An ArrayList of UserRoleInfo objects</returns>
        /// <history>
        /// [cnurse]	05/24/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public ArrayList GetUsersInRole(int PortalID, string RoleName)
        {
            string[] Users;
            string OriginalApplicationName = DotNetNuke.Common.Globals.GetApplicationName();
            ArrayList arr = new ArrayList();
            try
            {
                DotNetNuke.Common.Globals.SetApplicationName(PortalID);
                Users = Roles.GetUsersInRole(RoleName);
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(OriginalApplicationName);
            }
            RoleController objRoleController = new RoleController();
            int refIntHelperL0 = Users.Length - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                IEnumerator refObjectHelperL0=null;
                ArrayList arrUserRoles = objRoleController.GetUserRolesByUsername(PortalID, Users[i], RoleName);
                try
                {
                    refObjectHelperL0 = arrUserRoles.GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        UserRoleInfo objUserRole = (UserRoleInfo) refObjectHelperL0.Current;
                        arr.Add(objUserRole);
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
            }
            return arr;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Synchronize the DNN Roles and AspNet Roles
        /// </summary>
        /// <param name="PortalID"></param>
        /// <history>
        /// [cnurse]	05/23/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public void SynchronizeRoles(int PortalID)
        {
            string[] Roles;
            string OriginalApplicationName = DotNetNuke.Common.Globals.GetApplicationName();
            RoleController objRoleController = new RoleController();
            try
            {
                DotNetNuke.Common.Globals.SetApplicationName(PortalID);
                Roles = System.Web.Security.Roles.GetAllRoles();
            }
            finally
            {
                DotNetNuke.Common.Globals.SetApplicationName(OriginalApplicationName);
            }
            int refIntHelperL0 = Roles.Length - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                if (objRoleController.GetRoleByName(PortalID, Roles[i]) == null)
                {
                    RoleInfo objRoleInfo = new RoleInfo();
                    objRoleInfo.PortalID = PortalID;
                    objRoleInfo.RoleName = Roles[i];
                    objRoleController.AddRole(objRoleInfo, true);
                }
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Persists a role to the Data Store
        /// </summary>
        /// <param name="objRoleInfo">The role to persist</param>
        /// <history>
        /// [cnurse]	05/24/2005	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public void UpdateRole(RoleInfo objRoleInfo)
        {
            DataProvider.Instance().UpdateRole(objRoleInfo.RoleID, objRoleInfo.Description, objRoleInfo.ServiceFee, objRoleInfo.BillingPeriod.ToString(), objRoleInfo.BillingFrequency, objRoleInfo.TrialFee, objRoleInfo.TrialPeriod, objRoleInfo.TrialFrequency, objRoleInfo.IsPublic, objRoleInfo.AutoAssignment);
            this.AutoAssignUsers(objRoleInfo, objRoleInfo.RoleID, false);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Updates a Service (UserRole)
        /// </summary>
        /// <param name="PortalID">The Id of the Portal</param>
        /// <param name="UserId">The Id of the User</param>
        /// <param name="RoleId">The Id of the Role</param>
        /// <history>
        /// [Nik Kalyani]	10/15/2004	Created multiple signatures to eliminate Optional parameters
        /// </history>
        /// -----------------------------------------------------------------------------
        public void UpdateService(int PortalId, int UserId, int RoleId)
        {
            this.UpdateService(PortalId, UserId, RoleId, false);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Updates a Service (UserRole)
        /// </summary>
        /// <param name="PortalID">The Id of the Portal</param>
        /// <param name="UserId">The Id of the User</param>
        /// <param name="RoleId">The Id of the Role</param>
        /// <param name="Cancel">A flag that indicates whether to cancel (delete) the userrole</param>
        /// <history>
        /// [Nik Kalyani]	10/15/2004	Created multiple signatures to eliminate Optional parameters
        /// </history>
        /// -----------------------------------------------------------------------------
        public void UpdateService(int PortalId, int UserId, int RoleId, bool Cancel)
        {
            if (Cancel)
            {
                this.DeleteUserRole(PortalId, UserId, RoleId);
            }
            else
            {
                int Period=0;
                int UserRoleId = -1;
                DateTime ExpiryDate = DateAndTime.Now;
                bool IsTrialUsed = false;
                string Frequency = "";
                IDataReader dr = DataProvider.Instance().GetUserRole(PortalId, UserId, RoleId);
                if (dr.Read())
                {
                    UserRoleId = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["UserRoleId"]));
                    if (!Information.IsDBNull(RuntimeHelpers.GetObjectValue(dr["ExpiryDate"])))
                    {
                        ExpiryDate = Convert.ToDateTime(RuntimeHelpers.GetObjectValue(dr["ExpiryDate"]));
                    }
                    if (!Information.IsDBNull(RuntimeHelpers.GetObjectValue(dr["IsTrialUsed"])))
                    {
                        IsTrialUsed = Convert.ToBoolean(RuntimeHelpers.GetObjectValue(dr["IsTrialUsed"]));
                    }
                }
                dr.Close();
                dr = DataProvider.Instance().GetRole(RoleId, PortalId);
                if (dr.Read())
                {
                    if (!IsTrialUsed & (dr["TrialFrequency"].ToString() != "N"))
                    {
                        Period = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["TrialPeriod"]));
                        Frequency = dr["TrialFrequency"].ToString();
                    }
                    else
                    {
                        Period = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["BillingPeriod"]));
                        Frequency = dr["BillingFrequency"].ToString();
                    }
                }
                dr.Close();
                if (DateTime.Compare(ExpiryDate, DateAndTime.Now) < 0)
                {
                    ExpiryDate = DateAndTime.Now;
                }
                switch (Frequency)
                {
                    case "N":
                        ExpiryDate = Null.NullDate;
                        break;

                    case "O":
                        ExpiryDate = new DateTime(0x270f, 12, 0x1f);
                        break;

                    case "D":
                        ExpiryDate = DateAndTime.DateAdd(DateInterval.Day, (double) Period, Convert.ToDateTime(ExpiryDate));
                        break;

                    case "W":
                        ExpiryDate = DateAndTime.DateAdd(DateInterval.Day, (double) (Period * 7), Convert.ToDateTime(ExpiryDate));
                        break;

                    case "M":
                        ExpiryDate = DateAndTime.DateAdd(DateInterval.Month, (double) Period, Convert.ToDateTime(ExpiryDate));
                        break;

                    case "Y":
                        ExpiryDate = DateAndTime.DateAdd(DateInterval.Year, (double) Period, Convert.ToDateTime(ExpiryDate));
                        break;
                }
                if (UserRoleId != -1)
                {
                    DataProvider.Instance().UpdateUserRole(UserRoleId, ExpiryDate);
                }
                else
                {
                    this.AddUserRole(PortalId, UserId, RoleId, ExpiryDate);
                }
            }
        }
    }
}

